package yso.payloads.exploitType;


import org.reflections.Reflections;

import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.Set;


@SuppressWarnings("rawtypes")
public interface EXP<T> {

    /*
     * return armed payload object to be serialized that will execute specified
     * command on deserialization
     */
    public T getObject() throws Exception;

    public static class Utils {

        // get payload classes by classpath scanning
        public static Set<Class<? extends EXP>> getPayloadClasses() {
            final Reflections reflections = new Reflections(EXP.class.getPackage().getName());
            final Set<Class<? extends EXP>> payloadTypes = reflections.getSubTypesOf(EXP.class);
            for (Iterator<Class<? extends EXP>> iterator = payloadTypes.iterator(); iterator.hasNext(); ) {
                Class<? extends EXP> pc = iterator.next();
                if (pc.isInterface() || Modifier.isAbstract(pc.getModifiers())) {
                    iterator.remove();
                }
            }
            return payloadTypes;
        }


        @SuppressWarnings ( "unchecked" )
        public static Class<? extends EXP> getPayloadClass (final String className ) {
            Class<? extends EXP> clazz = null;
            try {
                clazz = (Class<? extends EXP>) Class.forName(className);
            }
            catch ( Exception e1 ) {}
            if ( clazz == null ) {
                try {
                    return clazz = (Class<? extends EXP>) Class
                            .forName("yso.payloads.exploitType." + className);
                }
                catch ( Exception e2 ) {}
            }
            if ( clazz != null && !EXP.class.isAssignableFrom(clazz) ) {
                clazz = null;
            }
            return clazz;
        }

    }
}